package com.cyj.service;

import com.alibaba.fastjson.JSON;
import com.cyj.pojo.Content;
import com.cyj.utils.HtmlParseUtil;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
//业务编写
public class ContentService {
    @Autowired
    private RestHighLevelClient restHighLevelClient;

    //存储数据
    public Boolean parseContent(String keywords) throws IOException {
        List<Content> contents=new HtmlParseUtil().parseJD(keywords);
        //把查询到的数据存入es中
        BulkRequest bulkRequest=new BulkRequest();
        bulkRequest.timeout("2m");
        //批量处理请求
        for(int i=0;i<contents.size();i++){
            //批量更新和删除，在这里修改即可
            bulkRequest.add(
                    new IndexRequest("jd_goods")
//                            .id(""+(i+1))//删掉后是随机id
                            .source(JSON.toJSONString(contents.get(i)), XContentType.JSON)
            );

        }
        BulkResponse bulkItemResponses=restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
       return !bulkItemResponses.hasFailures();
    }
    //2.获取这些数据实现搜索功能
    public List<Map<String,Object>> searchPage(String keyword,int pageNo,int pageSize) throws IOException {
        if(pageNo<=1){
            pageNo=1;
        }
        //条件搜索
        SearchRequest searchRequest=new SearchRequest("jd_goods");
        SearchSourceBuilder sourceBuilder=new SearchSourceBuilder();
        //分页
        sourceBuilder.from(pageNo);
        sourceBuilder.size(pageSize);
        //精准匹配
        TermQueryBuilder termQueryBuilder= QueryBuilders.termQuery("title",keyword);
        sourceBuilder.query(termQueryBuilder);
        sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
        //执行搜索
        searchRequest.source(sourceBuilder);
        SearchResponse searchResponse=restHighLevelClient.search(searchRequest,RequestOptions.DEFAULT);
        //解析结果
        ArrayList<Map<String,Object>>list=new ArrayList<>();
        for(SearchHit documentFields:searchResponse.getHits().getHits()){
            list.add(documentFields.getSourceAsMap());
        }
        return list;
    }
    //2.获取这些数据实现搜索功能--高亮
    public List<Map<String,Object>> searchPageHighLight(String keyword,int pageNo,int pageSize) throws IOException {
        if(pageNo<=1){
            pageNo=1;
        }
        //条件搜索
        SearchRequest searchRequest=new SearchRequest("jd_goods");
        SearchSourceBuilder sourceBuilder=new SearchSourceBuilder();
        //分页
        sourceBuilder.from(pageNo);
        sourceBuilder.size(pageSize);
        //精准匹配
        TermQueryBuilder termQueryBuilder= QueryBuilders.termQuery("title",keyword);
        sourceBuilder.query(termQueryBuilder);
        sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
        //高亮
        // 高亮构建！
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        //生成高亮查询器
        highlightBuilder.field("title"); //高亮查询字段
        highlightBuilder.requireFieldMatch(false);//如果要多个字段高亮,这项要为false
        highlightBuilder.preTags("<span style=\"color:red\">");  //高亮设置
        highlightBuilder.postTags("</span>");
        sourceBuilder.highlighter(highlightBuilder);
        sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
        //执行搜索
        searchRequest.source(sourceBuilder);
        SearchResponse searchResponse=restHighLevelClient.search(searchRequest,RequestOptions.DEFAULT);
        //解析结果
//        ArrayList<Map<String,Object>>list=new ArrayList<>();
        // 解析结果！
        List<Map<String, Object>> list = new ArrayList<>();
        for (SearchHit hit : searchResponse.getHits()) {
            //获取高亮字段
            Map<String, HighlightField> highlightFields = hit.getHighlightFields();
            HighlightField titleField = highlightFields.get("title");
            Map<String, Object> source = hit.getSourceAsMap();
            //千万记得要记得判断是不是为空,不然你匹配的第一个结果没有高亮内容,那么就会报空指 针异常,这个错误一开始真的搞了很久
            //解析高亮字段，将烟来的字段替换为我们的高亮字段
            if(titleField!=null){
                Text[] fragments = titleField.fragments();
                 String name = "";
                 for (Text text : fragments) {
                     name += text;
                 }
                 source.put("title", name); //高亮字段替换掉原本的内容
            }
            list.add(source);
        }
        return list;
    }
}
